package com.gouwudai.gobbs.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IAreaDAO;
import com.gouwudai.gobbs.pojo.TbbsArea;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
import com.litt.core.exception.DBException;

/**
 * 
 * <pre>
 *  Title:地区信息DAO
 *  Description: 地区信息DAO  
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2007-3-29
 * @version 1.0
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容: 
 * </pre>
 */
public class AreaDAOImpl extends BaseDAOImpl implements IAreaDAO 
{
	private static final Logger logger = Logger.getLogger(AreaDAOImpl.class);	
	private static final String BUSI_NAME = "地区信息";	
	
	public void save(TbbsArea tbbsArea)
	{
        super.saveObject(tbbsArea);		
	}
	
	public TbbsArea load(Integer id)
	{
		return (TbbsArea)super.loadObject(TbbsArea.class, id);
	}   
    
    public void update(TbbsArea tbbsArea)
    {
        StringBuilder hql = new StringBuilder();
        hql.append("update TbbsArea set areaName=?");
        hql.append(",parentId=?");
        hql.append(" where areaId=?");
        super.updateObject(hql.toString(), new Object[]{
        	tbbsArea.getAreaName(), 
        	tbbsArea.getParentId(),
        	tbbsArea.getAreaId()});

    }

    public void delete(Integer cfId)
    {
        super.deleteObject(TbbsArea.class.getName(),"areaId",cfId);
        super.deleteObject(TbbsArea.class.getName(),"parentId",cfId);
    }
    
    public void deleteSome(String cfIds)
    {
        super.deleteObjects(TbbsArea.class.getName(),"areaId",cfIds);
    } 
    
    
    public List listByType(int areaType)
    {
    	String hql = "from TbbsArea obj where obj.areaType="+areaType;
    	return super.listAll(hql);
    }
    
    /**
     * 根据省地区ID获得下属城市
     * @param areaId
     * @return
     * @throws DBException
     */
    public List listSubCities(Integer areaId)
    {
    	String hql = "select obj.areaName from TbbsArea obj where obj.parentId="+areaId;
    	return super.listAll(hql);
    }    

    /**
     * 分页查找
     * @param pageIndex
     * @param pageSize
     * @param sort
     * @return
     */
    public IPageList listPage(int pageIndex,int pageSize,String sort)
    {       
    	StringBuffer listSql = new StringBuffer(200);
        listSql.append("SELECT A.*,B.DICT_CONTENT AS AREA_TYPE_NAME,'无' AS PARENT_AREA_NAME FROM TbbsArea A,TbbsDictParam B WHERE A.PARENT_ID=0 AND B.DICT_TYPE='S002' AND A.AREA_TYPE=B.DICT_VALUE");
        listSql.append(" UNION ALL SELECT C.*,D.DICT_CONTENT AS AREA_TYPE_NAME,E.AREA_NAME AS PARENT_AREA_NAME FROM TbbsArea C,TbbsDictParam D,TbbsArea E WHERE C.PARENT_ID<>0 AND C.PARENT_ID=E.AREA_ID AND D.DICT_TYPE='S002' AND C.AREA_TYPE=D.DICT_VALUE");
        if(!Utility.isEmpty(sort))
            listSql.append(sort);
    	return super.listSqlPage(listSql.toString(),pageIndex,pageSize);
    }    
}
